package com.frame.work.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.frame.work.entity.TblMeteringLog;
import com.frame.work.entity.dto.TblMeteringDTO;
import com.frame.work.entity.dto.TblMeteringMessageDTO;
import com.frame.work.service.TblMeteringLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/metering")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MeteringLogController {


    private final TblMeteringLogService meteringLogService;

    /**
     * 通过时间查询
     *
     * @return 单条数据
     */
    @GetMapping("/device")
    public R selectLog(@RequestParam("deviceMac") String deviceMac,
                       @RequestParam("start") Long start,
                       @RequestParam("end") Long end) {


        List<TblMeteringLog> tblMeteringLogs1 = meteringLogService.selectByTime(deviceMac, new Date(start), new Date(end));

        List<TblMeteringDTO> tblMeteringLogs = new ArrayList<>();
        for (TblMeteringLog meteringLog : tblMeteringLogs1) {
            TblMeteringDTO tblMeteringLogDTO = new TblMeteringDTO();
            BeanUtils.copyProperties(meteringLog, tblMeteringLogDTO);
            tblMeteringLogs.add(tblMeteringLogDTO);
        }
        return R.ok(tblMeteringLogs);

    }


    /**
     * 查询设备的电量数据，包含日电量、周电量和月电量
     *
     * @return 单条数据
     */
    @GetMapping("/device/message")
    public R selectLogMessage(@RequestParam("deviceMac") String deviceMac,
                              @RequestParam("start") Long start,
                              @RequestParam("end") Long end) {

        TblMeteringMessageDTO tblMeteringMessageDTO = new TblMeteringMessageDTO();

        //查询当日电量
        List<TblMeteringLog> tblDayMeterLogs = meteringLogService.selectByTime(deviceMac, new Date(start), new Date(end));
        List<TblMeteringDTO> tblMeteringLogs = new ArrayList<>();
        for (TblMeteringLog meteringLog : tblDayMeterLogs) {
            TblMeteringDTO tblMeteringLogDTO = new TblMeteringDTO();
            BeanUtils.copyProperties(meteringLog, tblMeteringLogDTO);
            tblMeteringLogs.add(tblMeteringLogDTO);
        }

        double valueOfDay = 0;
        double valueOfMonth = 0;
        double valueOfWeek = 0;

        if (tblDayMeterLogs.size() > 0) {
            //提供当天第一条数据值，本日最近时候的用电量值减去本日最远时候的用电量
            List<Double> dayElectric = new ArrayList<>(tblDayMeterLogs.size());
            for (TblMeteringLog tblSecurityLog : tblDayMeterLogs) {
                dayElectric.add(tblSecurityLog.getElectricQuantity());
            }
            Collections.reverse(dayElectric);
            valueOfDay = meteringLogService.getElectricConsume(dayElectric);
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(start));
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        String yearStr = Integer.toString(year);
        String monthStr = "";
        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = Integer.toString(month);
        }
        //查询当月电量
        List<Double> monthElectric = meteringLogService.selectElectricNumByMonth(yearStr, monthStr, deviceMac);


        if (!CollectionUtils.isEmpty(monthElectric)) {
            //需要反转
            Collections.reverse(monthElectric);
            valueOfMonth = meteringLogService.getElectricConsume(monthElectric);
        } else {
            valueOfMonth = 0;
        }


        //查询本周电量
        List weekStartAndEndTime = meteringLogService.getWeekStartAndEndTime(start);
        long weekStartTime = (long) weekStartAndEndTime.get(0);
        long weekEndTime = (long) weekStartAndEndTime.get(1);
        List<TblMeteringLog> tblMeteringLogsOfWeek = meteringLogService.selectByTime(deviceMac, new Date(weekStartTime), new Date(weekEndTime));

        if (tblMeteringLogsOfWeek.size() == 0) {
            valueOfWeek = 0;
        } else {
            List<Double> weekElectric = new ArrayList<>(tblMeteringLogsOfWeek.size());
            for (TblMeteringLog tblMeteringLog : tblMeteringLogsOfWeek) {
                weekElectric.add(tblMeteringLog.getElectricQuantity());
            }
            Collections.reverse(weekElectric);
            valueOfWeek = meteringLogService.getElectricConsume(weekElectric);
        }


        //以下为新的，只提供三个日期初始值参数
        //当天电量赋值
        tblMeteringMessageDTO.setElectricQuantityOfToday(round(valueOfDay));
        //当周电量赋值
        tblMeteringMessageDTO.setElectricQuantityOfWeek(round(valueOfWeek));
        //当月电量赋值
        tblMeteringMessageDTO.setElectricQuantityOfMonth(round(valueOfMonth));
        //list赋值
        tblMeteringMessageDTO.setList(tblMeteringLogs);
        return R.ok(tblMeteringMessageDTO);

    }

    /**
     * double类型数值四舍五入
     */

    public double round(double v) {
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 根据deviceMac删除全部日志
     *
     * @return
     */

    @DeleteMapping("/deleteAll/deviceMac")
    public String deleteAllTblSecurityByDeviceMac(@RequestParam("deviceMac") String deviceMac) {
        int i = this.meteringLogService.deleteAllByDeviceMac(deviceMac);
        if (i > 0) {
            return "删除成功";
        } else {
            return "删除失败";
        }

    }

    /**
     * 根据日志id批量删除日志
     *
     * @return
     */
    @DeleteMapping("/delete/ids")
    @Transactional
    public String deleteTblSecurityByIds(@RequestBody List<String> ids) {
        int i = this.meteringLogService.deleteByIds(ids);
        if (i > 0) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }
}
